package com.test.sdk.core.controller;

import com.test.sdk.common.pojo.CP;
import com.test.sdk.common.pojo.Game;
import com.test.sdk.common.pojo.User;
import com.test.sdk.common.util.ErrorConstants;
import com.test.sdk.common.util.ResponseTO;
import com.test.sdk.core.cache.CPCache;
import com.test.sdk.core.cache.GameCache;
import com.test.sdk.core.exception.SdkException;
import com.test.sdk.core.service.RedisService;
import com.test.sdk.core.service.UserService;
import com.test.sdk.core.util.*;
import org.apache.tomcat.jni.Error;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 徒有琴
 */
@RestController
public class UserController implements ErrorConstants, SdkConstants {

    @Autowired
    private UserService userService;

    @RequestMapping("/user/reg/sendcode.html")
    public ResponseTO send(String account) throws SdkException {
        if (!AccountUtil.checkMobile(account)) {
            throw new SdkException(INVALID_MOBILE_INPUT);
        }
        boolean result = userService.doSendShortMessage(account, false);
        ResponseTO res = new ResponseTO();
        res.setSuccess(true);
        res.setResult(result ? "发送成功" : "发送失败");
        return res;
    }

    @RequestMapping("/user/register.html")
    public ResponseTO regist(String account, String password, String repassword, String code, Integer prtchid, Integer cpid) throws SdkException {
        User user = null;
        switch (AccountUtil.getUserType(account)) {
            case ACCOUNT_TYPE_USERNAME://用户名
                if (StringUtils.isEmpty(password) || StringUtils.isEmpty(repassword)) {
                    throw new SdkException(PARAM_ERROR);
                }
                if (!password.equals(repassword)) {
                    throw new SdkException(PASSWORD_REPASSWORD_UNEQUAL);
                }
                CP cp = CPCache.getCpById(cpid);
                try {
                    password = DES.decode(password, Base64.encode(cp.getSecretKey().getBytes()));
                    System.out.println(password);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new SdkException(INVALID_PASSWORD_INPUT);
                }
                user = userService.doUsernameRegist(account, password, prtchid);
                break;
            case ACCOUNT_TYPE_MOBILE://手机号
                if (StringUtils.isEmpty(code)) {
                    throw new SdkException(PARAM_ERROR);
                }
                user = userService.doMobileRegist(account, code, prtchid);
                break;
            default:
                throw new SdkException(INVALID_ACCOUNT_INPUT);
        }
        return ResponseTO.getOkResult(user);
    }

    @RequestMapping("/user/login.html")
    public ResponseTO login(String account, String password, Integer cpid, Integer seqnum) throws SdkException {
        switch (AccountUtil.getUserType(account)) {
            case ACCOUNT_TYPE_USERNAME://用户名
                if (StringUtils.isEmpty(password)) {
                    throw new SdkException(PARAM_ERROR);
                }
                CP cp = CPCache.getCpById(cpid);
                try {
                    password = DES.decode(password, Base64.encode(cp.getSecretKey().getBytes()));
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new SdkException(INVALID_PASSWORD_INPUT);
                }
                Game game = GameCache.getGameByCpIdAndSeqNum(cpid, seqnum);
                String ticket = userService.doUsernameLogin(account, password, game.getId());
                return ResponseTO.getOkResult(null, TicketUtil.encode(ticket));
            case ACCOUNT_TYPE_MOBILE://手机号,根据验证码
                return ResponseTO.getOkResult(null);
            default:
                throw new SdkException(INVALID_ACCOUNT_INPUT);
        }
    }

    @RequestMapping("/user/service/heartbeat.html")
    public ResponseTO heartbeat(String ticket) {
        ticket = TicketUtil.decode(ticket);
        userService.heartBeat(ticket);
        return ResponseTO.getOkResult(null, TicketUtil.encode(ticket));
    }
}
